﻿using System.Collections.Generic;
using System.Linq;
using Sovigaz.DataProvider;

namespace Sovigaz.Permission.Repositories
{
    public class UserRoleRepository : GenericRepository
    {
        public UserRoleRepository()
            : base("UserID-RoleID")
        {
        }

        public List<UserRole> GetByUserID(int userID)
        {
            return _context.UserRoles.Where(p => p.UserID == userID).ToList();
        }

        public List<UserRoleComplex> GetByRoleID(int roleID)
        {
            //return _context.UserRoles.Join(_context.Users, ur => ur.UserID, u => u.UserID, (ur, u) => new UserRoleComplex
            //{
            //    UserID = ur.UserID,
            //    Username = u.Username,
            //    FullName = u.FullName,
            //    RoleID = ur.RoleID,
            //}).Where(p => p.RoleID == roleID).ToList();
            return _context.UserRoles.Join(_context.Users, ur => ur.UserID, u => u.UserID,
                (ur, u) => new { ur.RoleID, ur.UserID, u.Username, u.FullName }).ToList().Where(p => p.RoleID == roleID)
                .Select(p => new UserRoleComplex
                {
                    UserID = p.UserID,
                    Username = p.Username,
                    FullName = p.FullName,
                    RoleID = p.RoleID,
                }).ToList();
        }
    }
}
